//
//  AudioClassTableViewController.m
//  To Feel
//
//  Created by dllo on 16/7/15.
//  Copyright © 2016年 caoyu. All rights reserved.
//
#import "AudioClassTableViewController.h"
#import "CellOfAudioClass.h"
#import "ModelOfAudioClassify.h"
#import "NetworkingHandler.h"
#import "AudioCollectionViewController.h"
@interface AudioClassTableViewController ()
@property (nonatomic , retain)UITableView *mainTableView;
@property (nonatomic,retain)NSMutableArray *arrOfTopic;
@property (nonatomic,retain)NSMutableArray *arrOfmodel;
@property (nonatomic, retain)NSMutableArray *arrOfList;

@end

@implementation AudioClassTableViewController
#define  DOCUMENT  [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
- (instancetype)initWithStyle:(UITableViewStyle)style {
    
    self = [super initWithStyle:style];
    
    if (self) {
        
        [self configTabBarItem];
        
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTableView];
    [self.tableView registerClass:[CellOfAudioClass class] forCellReuseIdentifier:@"pool"];
    self.navigationItem.title = @"音频";
}

- (void)configTabBarItem {
    
    self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"推荐" image:
                       
                       [UIImage imageNamed:@"faxian.png"] tag:200];
}

- (void)createTableView{
    NetworkingHandler *nt = [[NetworkingHandler alloc]init];
    
    self.arrOfList = [NSMutableArray array];
    
    NSString *str = @"http://mobile.ximalaya.com/mobile/discovery/v1/recommends?channel=and-d3&device=android&includeActivity=true&includeSpecial=true&scale=2&version=4.3.26.2";
    
    self.arrOfmodel = [NSMutableArray array];
    [nt netWorkingHandlerGETWithURL:str completion:^(id result, NSData *data, NSURLResponse *response, NSError *error) {
        
        NSArray *arr = [[result objectForKey:@"hotRecommends"] objectForKey:@"list"];
        for (NSDictionary *dic in arr) {
            ModelOfAudioClassify *model = [[ModelOfAudioClassify alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrOfmodel addObject:model];
            
            NSArray *arr = [dic objectForKey:@"list"];
            [self.arrOfList addObjectsFromArray:arr];
            
        }
        [self.tableView reloadData];
          [self createCollectionView];
    }];
    
}
- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((SCREEN_WIDTH - 40) / 3, 110);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumInteritemSpacing = 9.99;
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    
    for (int i = 0; i < self.arrOfmodel.count; i++) {
        
        AudioCollectionViewController *audio = [[AudioCollectionViewController alloc] initWithCollectionViewLayout:layout];
        [self addChildViewController:audio];
    }
}
#pragma mark - 存值
        // 写入文件
#pragma mark - 热门推荐
    
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.arrOfmodel.count;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AudioCollectionViewController *audio = self.childViewControllers[indexPath.section];
    CellOfAudioClass *cell = [tableView dequeueReusableCellWithIdentifier:@"pool" forIndexPath:indexPath];
    audio.collectionView.frame = cell.viewOfCollec.bounds;
    [cell.viewOfCollec addSubview:audio.collectionView];
    ModelOfAudioClassify *model = [self.arrOfmodel objectAtIndex:indexPath.section];
    audio.arrOfList = model.list;
    NSLog(@"%ld", indexPath.section);
    cell.model = self.arrOfmodel[indexPath.section];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 230;
}
@end
